// pages/musicPlay/musicPlay.js
import request from '../../utils/request'


//这个是用
var app = getApp();
//const voice = wx.createInnerAudioContext()
const bgm = wx.createInnerAudioContext()
//const bgm = wx.getBackgroundAudioManager()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        collectionList: [], // 歌曲详情
        url: [],//歌曲url
        id: Number,
        image: String,
        url2: String,
        isPlay: false,
        songs: String,
        songsList: [],
        type: Number,
        idx: Number,//用于表示歌单的index
        songs: 0,
        text: String,
        percent: Number,
        realTime: null,//实时数据对象(用于关闭实时刷新方法)
        timeLong: 999,//当前音乐播放时长
        nowTime: Number,//现在播放位置
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        bgm.volume = 1;
        console.log(options);

        let idx = options.idx;
        let type = options.type;
        this.setData({
            type,
            idx,
            songs,
        })
        let id = options.id;
        let songs = options.songs; //用于存放专辑或者歌单的id
        console.log(songs);
        console.log(idx);
        this.setData({ id });
        this.getCollectionList(id);
        let that = this
        
        this.data.realTime = setInterval(function () {
            // 请求服务器数据
            console.log('请求接口：刷新数据')
            let percent = 100 * bgm.currentTime / bgm.duration
            let nowTime = bgm.currentTime
            let timeLong = bgm.duration;
            console.log(timeLong);
           
            console.log(percent)
            that.setData({
                percent,
                nowTime,
                timeLong,
            })
        }, 500)//间隔时间 
        // 更新数据



        //这里分传参类型使用不同的绑定函数，暂定1为专辑，2为歌单
        if (type == 1) {
            this.getSongsList(songs);
        }
        else if (type == 2) {
            this.getSongsList2(songs);
            //这里使用歌单的获取方法
        }
        else if (type == 3) {
            //这里不知道是什么的获取方法
        }
        else {
            //不存在的异常处理
        }




        //下面是启用监听器
        app.setWatcher(this);
        setTimeout(() => {
            this.setData({
                name: "kangbosodoa"
            })
        }, 2000)
    },
    watch: {
        name: {
            handler(newValue, oldvalue) {
                //console.log(this)
                //console.log(newValue, oldvalue, "变化了");
                if (this.data.isPlay) {
                    bgm.src = this.data.url2
                    //bgm.title = this.data.collectionList.songs[0].name
                }
            },
            deep: true
        },
        type: {
            handler(newValue) {
                console.log(newValue, "属性发生变化");
            },
            deep: true // 是否深度监听
        }

    },

    //这是专辑情况的歌单获取
    async getSongsList(id) {
        let songsList = await request('/album', { id: id });
        this.setData({
            songsList,
        })
        console.log(this.data.songsList)
    },
    //这是歌单情况的歌单获取
    async getSongsList2(id) {
        let songsList = await request('/playlist/detail', { id: id });
        this.setData({
            songsList,
        })
        console.log(this.data.songsList)
    },

    slide(e) {
        console.log(e.detail.value);
        let a = e.detail.value / 10
        console.log(a);
        //voice.volume = a;
        bgm.volume = a;
        //这里需要将滑块与音量绑定
    },
    slide2(e) {
        let a = e.detail.value
        bgm.seek(a)

    },



    async getCollectionList(id) {//这个方法用来根据id改变当前播放歌曲
        let collectionList = await request('/song/detail', { ids: id });
        let url = await request('/song/url', { id: id });
        var allRequire = [];

        this.setData({
            collectionList,
            url,
            image: collectionList.songs[0].al.picUrl,
            url2: url.data[0].url,
        })
        allRequire.push(this.data.url2);

        Promise.all(allRequire).then(res => {
            console.log(res);//这里的res是一个数组，分别是上面三个请求的返回值
            let that = this;

            wx.setNavigationBarTitle({
                title: that.data.collectionList.songs[0].name,
            })
            if (this.data.isPlay) {
                bgm.src = this.data.url2
                bgm.play();

                //bgm.title = this.data.collectionList.songs[0].name
            }
        })

        //    // this.play();
        //     setTimeout(() => {
        //         if (!this.data.isPlay)
        //     {
        //         this.play();
        //     }
        //     }, 300);

        console.log(this.data.collectionList)
        console.log(this.data.url)

    },
    play() {
        if (!this.data.isPlay) {
            bgm.src = this.data.url2
            bgm.play();
            this.setVoice();
            
            // bgm.title = this.data.collectionList.songs[0].name
            this.data.isPlay = true
            console.log('已经开启了')
            console.log('请求接口：刷新数据(无条件执行)')


            this.setData({
                isPlay: this.data.isPlay
            })

        }
        else if (this.data.isPlay) {
            bgm.pause()
            this.data.isPlay = false
            console.log('已经关闭了')
            this.setData({
                isPlay: this.data.isPlay
            })
        }



    },
    setVoice() {


    },

    async getUrl(id) {
        console.log(id);
        let url = await request('/song/url', { id: id });
        console.log(url);
        console.log(this.data.url);
        this.setData({
            url,
            url2: url.data[0].url,
        })
        console.log(this.data.url2)
    },

    //这个方法用来播放下一首曲子
    next() {

        if (this.data.type == 1)//为专辑的情况
        {
            let xyz = this.data.collectionList.songs[0].no;
            //console.log(this.data.songsList.songs[xyz].al.id);
            let id = this.data.songsList.songs[xyz].id;
            console.log(id);
            this.getCollectionList(id);
        }
        else if (this.data.type == 2)//为歌单的情况
        {
            //console.log(this.data.idx);

            let idx = +this.data.idx + 1;
            //console.log(idx);
            this.setData({
                idx,
            })
            //console.log(this.data.songsList);
            let id = this.data.songsList.privileges[idx].id;
            console.log(id)
            this.getCollectionList(id);
            this.data.text = id;


        }
        else {
            //其他情况
        }



    },
    last() {
        if (this.data.type == 1)//为专辑的情况
        {
            console.log(this.data.collectionList);
            let xyz = this.data.collectionList.songs[0].no - 2;
            console.log(xyz);
            console.log(this.data.songsList);
            //console.log(this.data.songsList.songs[xyz].al.id);
            let id = this.data.songsList.songs[xyz].id;
            console.log(id);
            this.getCollectionList(id);

        }
        else if (this.data.type == 2)//为歌单的情况
        {
            console.log(this.data.idx);

            let idx = +this.data.idx - 1;
            console.log(idx);
            this.setData({
                idx,
            })
            console.log(this.data.songsList);
            let id = this.data.songsList.privileges[idx].id;
            this.getCollectionList(id);


        }

    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        clearInterval(this.data.realTime);
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    onrefresh: function () {

        wx.showToast({

            title: '开始重新渲染',
            icon: 'success',
            duration: 2000

        });

        this.onLoad();

    },
})


